home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 July / CHIP 2006-07.2.iso / program / web_gelistirme / easyphp1-7_setup.exe / {app} / phpmyadmin / libraries / common.lib.php < prev    next >
Encoding:
PHP Script  |  2003-09-07  |  80.9 KB  |  1,962 lines

  1. <?php
  2. /* $Id: common.lib.php,v 1.273.2.1 2003/09/02 10:16:47 nijel Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Misc stuff and functions used by almost all the scripts.
  7.  * Among other things, it contains the advanced authentification work.
  8.  */
  9.  
  10. if (!defined('PMA_COMMON_LIB_INCLUDED')) {
  11.     define('PMA_COMMON_LIB_INCLUDED', 1);
  12.  
  13.     /**
  14.      * Order of sections for common.lib.php:
  15.      *
  16.      * in PHP3, functions and constants must be physically defined
  17.      * before they are referenced
  18.      *
  19.      * some functions need the constants of libraries/defines.lib.php
  20.      * and defines_php.lib.php
  21.      *
  22.      * the PMA_setFontSizes() function must be before the call to the
  23.      * libraries/auth/cookie.auth.lib.php library
  24.      *
  25.      * the include of libraries/defines.lib.php must be after the connection
  26.      * to db to get the MySql version
  27.      *
  28.      * the PMA_sqlAddslashes() function must be before the connection to db
  29.      *
  30.      * the authentication libraries must be before the connection to db but
  31.      * after the PMA_isInto() function
  32.      *
  33.      * the PMA_mysqlDie() function must be before the connection to db but
  34.      * after mysql extension has been loaded
  35.      *
  36.      * the PMA_mysqlDie() function needs the PMA_format_sql() Function
  37.      *
  38.      * ... so the required order is:
  39.      *
  40.      * - parsing of the configuration file
  41.      * - first load of the libraries/defines_php.lib.php library (won't get the
  42.      *   MySQL release number)
  43.      * - load of mysql extension (if necessary)
  44.      * - definition of PMA_sqlAddslashes()
  45.      * - definition of PMA_format_sql()
  46.      * - definition of PMA_mysqlDie()
  47.      * - definition of PMA_isInto()
  48.      * - definition of PMA_setFontSizes()
  49.      * - loading of an authentication library
  50.      * - db connection
  51.      * - authentication work
  52.      * - second load of the libraries/define.lib.php library to get the MySQL
  53.      *   release number)
  54.      * - other functions, respecting dependencies
  55.      */
  56.  
  57.     /**
  58.      * Minimum inclusion? (i.e. for the stylesheet builder)
  59.      */
  60.  
  61.     if (!isset($is_minimum_common)) {
  62.         $is_minimum_common = FALSE;
  63.     }
  64.  
  65.     /**
  66.      * Avoids undefined variables in PHP3
  67.      */
  68.     if (!isset($use_backquotes)) {
  69.         $use_backquotes   = 0;
  70.     }
  71.     if (!isset($pos)) {
  72.         $pos              = 0;
  73.     }
  74.  
  75.     /**
  76.      * Detects the config file we want to load
  77.      */
  78.     if (file_exists('./config.inc.developer.php')) {
  79.         $cfgfile_to_load = './config.inc.developer.php';
  80.     } else {
  81.         $cfgfile_to_load = './config.inc.php';
  82.     }
  83.  
  84.     /**
  85.      * Parses the configuration file and gets some constants used to define
  86.      * versions of phpMyAdmin/php/mysql...
  87.      */
  88.     $old_error_reporting = error_reporting(0);
  89.     include($cfgfile_to_load);
  90.     // Include failed
  91.     if (!isset($cfgServers) && !isset($cfg['Servers'])) {
  92.         // Creates fake settings
  93.         $cfg = array('DefaultLang'           => 'en-iso-8859-1',
  94.                         'AllowAnywhereRecoding' => FALSE);
  95.         // Loads the language file
  96.         include('./libraries/select_lang.lib.php');
  97.         // Sends the Content-Type header
  98.         header('Content-Type: text/html; charset=' . $charset);
  99.         // Displays the error message
  100.         ?>
  101. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  102.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  103. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $available_languages[$lang][2]; ?>" lang="<?php echo $available_languages[$lang][2]; ?>" dir="<?php echo $text_dir; ?>">
  104.  
  105. <head>
  106. <title>phpMyAdmin</title>
  107. <meta http-equiv="Content-Type" content="text/html; charset=<?php echo $charset; ?>" />
  108.  
  109. <style type="text/css">
  110. <!--
  111. body  {font-family: sans-serif; font-size: small; color: #000000; background-color: #F5F5F5}
  112. h1    {font-family: sans-serif; font-size: large; font-weight: bold}
  113. //-->
  114. </style>
  115. </head>
  116.  
  117.  
  118. <body bgcolor="#ffffff">
  119. <h1>phpMyAdmin - <?php echo $strError; ?></h1>
  120. <p>
  121.     <?php echo $strConfigFileError; ?><br /><br />
  122.     <a href="config.inc.php" target="_blank">config.inc.php</a>
  123. </p>
  124. </body>
  125.  
  126. </html>
  127.         <?php
  128.         exit();
  129.     }
  130.     error_reporting($old_error_reporting);
  131.     unset($old_error_reporting);
  132.     unset($cfgfile_to_load);
  133.  
  134.     /**
  135.      * Includes compatibility code for older config.inc.php revisions
  136.      * if necessary
  137.      */
  138.     if (!isset($cfg['FileRevision']) || (int) substr($cfg['FileRevision'], 13, 3) < 197) {
  139.         include('./libraries/config_import.lib.php');
  140.     }
  141.  
  142.     /**
  143.      * Includes the language file if it hasn't been included yet
  144.      */
  145.     if (!defined('PMA_SELECT_LANG_LIB_INCLUDED')) {
  146.         include('./libraries/select_lang.lib.php');
  147.     }
  148.  
  149.     if ($is_minimum_common == FALSE) {
  150.         /**
  151.          * Include MySQL wrappers.
  152.          */
  153.         include('./libraries/mysql_wrappers.lib.php');
  154.     }
  155.  
  156.     /**
  157.      * Gets constants that defines the PHP version number.
  158.      * This include must be located physically before any code that needs to
  159.      * reference the constants, else PHP 3.0.16 won't be happy.
  160.      */
  161.     include('./libraries/defines_php.lib.php');
  162.  
  163.     if ($is_minimum_common == FALSE) {
  164.         /**
  165.          * Define $is_upload
  166.          */
  167.           //$is_upload = (PMA_PHP_INT_VERSION >= 40000 && function_exists('ini_get'))
  168.           //     ? ((strtolower(ini_get('file_uploads')) == 'on' || ini_get('file_uploads') == 1) && intval(ini_get('upload_max_filesize')))
  169.                // loic1: php 3.0.15 and lower bug -> always enabled
  170.           //     : (PMA_PHP_INT_VERSION < 30016 || intval(@get_cfg_var('upload_max_filesize')));
  171.  
  172.           // Note: PHP <40300 returns TRUE for function_exists, even
  173.           // if the function is disabled;
  174.           // PHP < 30016 had problems with get_cfg_var;
  175.           //  so if we cannot detect that uploads
  176.           //  are disabled, we assume they are enabled
  177.  
  178.           $is_upload = TRUE;
  179.           if (PMA_PHP_INT_VERSION >= 40000
  180.               && function_exists('ini_get')
  181.               && (strtolower(@ini_get('file_uploads')) == 'off'
  182.                  || @ini_get('file_uploads') == 0)) {
  183.               $is_upload = FALSE;
  184.           }
  185.           if (PMA_PHP_INT_VERSION >= 30016
  186.               && PMA_PHP_INT_VERSION < 40000
  187.               && !intval(@get_cfg_var('upload_max_filesize'))) {
  188.               $is_upload = FALSE;
  189.           }
  190.  
  191.  
  192.         /**
  193.          * Charset conversion.
  194.          */
  195.         include('./libraries/charset_conversion.lib.php');
  196.     }
  197.  
  198.     /**
  199.      * Gets constants that defines the MySQL version number.
  200.      * This include must be located physically before any code that needs to
  201.      * reference the constants, else PHP 3.0.16 won't be happy; and must be
  202.      * located after we are connected to db to get the MySql version (see
  203.      * below).
  204.      */
  205.     include('./libraries/defines.lib.php');
  206.  
  207.     if ($is_minimum_common == FALSE) {
  208.         /**
  209.          * String handling
  210.          */
  211.         include('./libraries/string.lib.php');
  212.     }
  213.  
  214.     if ($is_minimum_common == FALSE) {
  215.         /**
  216.          * SQL Parser data
  217.          */
  218.         include('./libraries/sqlparser.data.php');
  219.     }
  220.  
  221.     /**
  222.      * SQL Parser code
  223.      */
  224.  
  225.     include('./libraries/sqlparser.lib.php');
  226.  
  227.     if ($is_minimum_common == FALSE) {
  228.         /**
  229.          * SQL Validator interface code
  230.          */
  231.         include('./libraries/sqlvalidator.lib.php');
  232.     }
  233.  
  234.     // If zlib output compression is set in the php configuration file, no
  235.     // output buffering should be run
  236.     if (PMA_PHP_INT_VERSION < 40000
  237.         || (PMA_PHP_INT_VERSION >= 40005 && @ini_get('zlib.output_compression'))) {
  238.         $cfg['OBGzip'] = FALSE;
  239.     }
  240.  
  241.     // disable output-buffering (if set to 'auto') for IE6, else enable it.
  242.     if (strtolower($cfg['OBGzip']) == 'auto') {
  243.         if (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER >= 6 && PMA_USR_BROWSER_VER < 7) {
  244.             $cfg['OBGzip'] = FALSE;
  245.         } else {
  246.             $cfg['OBGzip'] = TRUE;
  247.         }
  248.     }
  249.  
  250.     if ($is_minimum_common == FALSE) {
  251.         /**
  252.          * Include URL/hidden inputs generating.
  253.          */
  254.         include('./libraries/url_generating.lib.php');
  255.  
  256.  
  257.         /**
  258.          * Loads the mysql extensions if it is not loaded yet
  259.          */
  260.         if (!@function_exists('mysql_connect')) {
  261.             if (PMA_PHP_INT_VERSION < 40000) {
  262.                 $extension = 'MySQL';
  263.             } else {
  264.                 $extension = 'mysql';
  265.             }
  266.             PMA_dl($extension);
  267.         }
  268.  
  269.         // check whether mysql is available
  270.         if (!@function_exists('mysql_connect')) {
  271.             if (empty($is_header_sent)) {
  272.                 include('./libraries/header_http.inc.php');
  273.             }
  274.             echo $strCantLoadMySQL . '<br />' . "\n"
  275.                  . '<a href="./Documentation.html#faqmysql" target="documentation">' . $GLOBALS['strDocu'] . '</a>' . "\n";
  276.             exit();
  277.         }
  278.  
  279.  
  280.         /**
  281.          * Add slashes before "'" and "\" characters so a value containing them can
  282.          * be used in a sql comparison.
  283.          *
  284.          * @param   string   the string to slash
  285.          * @param   boolean  whether the string will be used in a 'LIKE' clause
  286.          *                   (it then requires two more escaped sequences) or not
  287.          * @param   boolean  whether to treat cr/lfs as escape-worthy entities
  288.          *                   (converts \n to \\n, \r to \\r)
  289.          *
  290.          * @return  string   the slashed string
  291.          *
  292.          * @access  public
  293.          */
  294.         function PMA_sqlAddslashes($a_string = '', $is_like = FALSE, $crlf = FALSE)
  295.         {
  296.             if ($is_like) {
  297.                 $a_string = str_replace('\\', '\\\\\\\\', $a_string);
  298.             } else {
  299.                 $a_string = str_replace('\\', '\\\\', $a_string);
  300.             }
  301.  
  302.             if ($crlf) {
  303.                 $a_string = str_replace("\n", '\n', $a_string);
  304.                 $a_string = str_replace("\r", '\r', $a_string);
  305.                 $a_string = str_replace("\t", '\t', $a_string);
  306.             }
  307.  
  308.             $a_string = str_replace('\'', '\\\'', $a_string);
  309.  
  310.             return $a_string;
  311.         } // end of the 'PMA_sqlAddslashes()' function
  312.  
  313.  
  314.         /**
  315.          * Add slashes before "_" and "%" characters for using them in MySQL
  316.          * database, table and field names.
  317.          * Note: This function does not escape backslashes!
  318.          *
  319.          * @param   string   the string to escape
  320.          *
  321.          * @return  string   the escaped string
  322.          *
  323.          * @access  public
  324.          */
  325.         function PMA_escape_mysql_wildcards($name)
  326.         {
  327.             $name = str_replace('_', '\\_', $name);
  328.             $name = str_replace('%', '\\%', $name);
  329.  
  330.             return $name;
  331.         } // end of the 'PMA_escape_mysql_wildcards()' function
  332.  
  333.  
  334.         /**
  335.          * format sql strings
  336.          *
  337.          * @param   mixed    pre-parsed SQL structure
  338.          *
  339.          * @return  string   the formatted sql
  340.          *
  341.          * @global  array    the configuration array
  342.          * @global  boolean  whether the current statement is a multiple one or not
  343.          *
  344.          * @access  public
  345.          *
  346.          * @author  Robin Johnson <robbat2@users.sourceforge.net>
  347.          */
  348.         function PMA_formatSql($parsed_sql, $unparsed_sql = '')
  349.         {
  350.             global $cfg;
  351.             
  352.             // Check that we actually have a valid set of parsed data
  353.             // well, not quite
  354.             // first check for the SQL parser having hit an error
  355.             if (PMA_SQP_isError()) {
  356.                 return $parsed_sql;
  357.             }
  358.             // then check for an array
  359.             if (!is_array($parsed_sql)) {
  360.                 // We don't so just return the input directly
  361.                 // This is intended to be used for when the SQL Parser is turned off
  362.                 $formatted_sql = '<pre>' . "\n"
  363.                                 . (($cfg['SQP']['fmtType'] == 'none' && $unparsed_sql != '') ? $unparsed_sql : $parsed_sql) . "\n"
  364.                                 . '</pre>';
  365.                 return $formatted_sql;
  366.             }
  367.  
  368.             $formatted_sql        = '';
  369.  
  370.             switch ($cfg['SQP']['fmtType']) {
  371.                 case 'none':
  372.                     if ($unparsed_sql != '') {
  373.                         $formatted_sql = "<pre>\n" . PMA_SQP_formatNone(array('raw' => $unparsed_sql)) . "\n</pre>";
  374.                     } else {
  375.                         $formatted_sql = PMA_SQP_formatNone($parsed_sql);
  376.                     }
  377.                     break;
  378.                 case 'html':
  379.                     $formatted_sql = PMA_SQP_formatHtml($parsed_sql,'color');
  380.                     break;
  381.                 case 'text':
  382.                     //$formatted_sql = PMA_SQP_formatText($parsed_sql);
  383.                     $formatted_sql = PMA_SQP_formatHtml($parsed_sql,'text');
  384.                     break;
  385.                 default:
  386.                     break;
  387.             } // end switch
  388.  
  389.             return $formatted_sql;
  390.         } // end of the "PMA_formatSql()" function
  391.  
  392.  
  393.         /**
  394.          * Displays a link to the official MySQL documentation
  395.          *
  396.          * @param   chapter of "HTML, one page per chapter" documentation
  397.          * @param   contains name of page/anchor that is being linked
  398.          *
  399.          * @return  string  the html link
  400.          *
  401.          * @access  public
  402.          */
  403.         function PMA_showMySQLDocu($chapter, $link)
  404.         {
  405.             if (!empty($GLOBALS['cfg']['MySQLManualBase'])) {
  406.                 if (!empty($GLOBALS['cfg']['MySQLManualType'])) {
  407.                     switch ($GLOBALS['cfg']['MySQLManualType']) {
  408.                         case 'old':
  409.                             return '[<a href="' . $GLOBALS['cfg']['MySQLManualBase'] . '/' . $link[0] . '/' . $link[1] . '/' . $link . '.html" target="mysql_doc">' . $GLOBALS['strDocu'] . '</a>]';
  410.                         case 'chapters':
  411.                             return '[<a href="' . $GLOBALS['cfg']['MySQLManualBase'] . '/manual_' . $chapter . '.html#' . $link . '" target="mysql_doc">' . $GLOBALS['strDocu'] . '</a>]';
  412.                         case 'big':
  413.                             return '[<a href="' . $GLOBALS['cfg']['MySQLManualBase'] . '#' . $link . '" target="mysql_doc">' . $GLOBALS['strDocu'] . '</a>]';
  414.                         case 'none':
  415.                             return '';
  416.                         case 'searchable':
  417.                         default:
  418.                             return '[<a href="' . $GLOBALS['cfg']['MySQLManualBase'] . '/' . $link . '.html" target="mysql_doc">' . $GLOBALS['strDocu'] . '</a>]';
  419.                     }
  420.                 } else {
  421.                     // no Type defined, show the old one
  422.                     return '[<a href="' . $GLOBALS['cfg']['MySQLManualBase'] . '/' . $link[0] . '/' . $link[1] . '/' . $link . '.html" target="mysql_doc">' . $GLOBALS['strDocu'] . '</a>]';
  423.                 }
  424.             } else {
  425.                 // no URL defined
  426.                 if (!empty($GLOBALS['cfg']['ManualBaseShort'])) {
  427.                     // the old configuration
  428.                     return '[<a href="' . $GLOBALS['cfg']['MySQLManualBase'] . '/' . $link[0] . '/' . $link[1] . '/' . $link . '.html" target="mysql_doc">' . $GLOBALS['strDocu'] . '</a>]';
  429.                 } else {
  430.                     return '';
  431.                 }
  432.             }
  433.         } // end of the 'PMA_showDocu()' function
  434.  
  435.         /**
  436.          * Displays a MySQL error message in the right frame.
  437.          *
  438.          * @param   string   the error mesage
  439.          * @param   string   the sql query that failed
  440.          * @param   boolean  whether to show a "modify" link or not
  441.          * @param   string   the "back" link url (full path is not required)
  442.          * @param   boolean  EXIT the page?
  443.          *
  444.          * @global  array    the configuration array
  445.          *
  446.          * @access  public
  447.          */
  448.         function PMA_mysqlDie($error_message = '', $the_query = '',
  449.                                 $is_modify_link = TRUE, $back_url = '',
  450.                                 $exit = TRUE)
  451.         {
  452.             global $cfg, $table, $db, $sql_query;
  453.  
  454.             if (empty($GLOBALS['is_header_sent'])) {
  455.                 include('./header.inc.php');
  456.             }
  457.  
  458.             if (!$error_message) {
  459.                 $error_message = PMA_mysql_error();
  460.             }
  461.             if (!$the_query && !empty($GLOBALS['sql_query'])) {
  462.                 $the_query = $GLOBALS['sql_query'];
  463.             }
  464.  
  465.             // --- Added to solve bug #641765
  466.             // Robbat2 - 12 January 2003, 9:46PM
  467.             // Revised, Robbat2 - 13 Janurary 2003, 2:59PM
  468.             if (PMA_SQP_isError()) {
  469.                 $parsed_sql = htmlspecialchars($the_query);
  470.             } else {
  471.                 $parsed_sql = PMA_SQP_parse($the_query);
  472.             }
  473.             // ---
  474.  
  475.             echo '<p><b>'. $GLOBALS['strError'] . '</b></p>' . "\n";
  476.             // if the config password is wrong, or the MySQL server does not
  477.             // respond, do not show the query that would reveal the
  478.             // username/password
  479.             if (!empty($the_query) && !strstr($the_query, 'connect')) {
  480.                 // --- Added to solve bug #641765
  481.                 // Robbat2 - 12 January 2003, 9:46PM
  482.                 // Revised, Robbat2 - 13 Janurary 2003, 2:59PM
  483.                 if (PMA_SQP_isError()) {
  484.                     echo PMA_SQP_getErrorString();
  485.                 }
  486.                 // ---
  487.                 echo '<p>' . "\n";
  488.                 echo '    ' . $GLOBALS['strSQLQuery'] . ' : ' . "\n";
  489.                 if ($is_modify_link && isset($db)) {
  490.                     echo '    ['
  491.                             . '<a href="db_details.php?' . PMA_generate_common_url($GLOBALS['db']) . '&sql_query=' . urlencode($the_query) . '&show_query=1">' . $GLOBALS['strEdit'] . '</a>'
  492.                             . ']' . "\n";
  493.                 } // end if
  494.                 echo '</p>' . "\n"
  495.                         . '<p>' . "\n"
  496.                         . '    ' . PMA_formatSql($parsed_sql, $the_query) . "\n"
  497.                         . '</p>' . "\n";
  498.             } // end if
  499.             if (!empty($error_message)) {
  500.                 $error_message = htmlspecialchars($error_message);
  501.                 $error_message = ereg_replace("((\015\012)|(\015)|(\012)){3,}", "\n\n", $error_message);
  502.             }
  503.             echo '<p>' . "\n"
  504.                     . '    ' . $GLOBALS['strMySQLSaid'] . '<br />' . "\n"
  505.                     . '</p>' . "\n";
  506.             echo '<pre>' . "\n"
  507.                     . $error_message . "\n"
  508.                     . '</pre>' . "\n";
  509.  
  510.             echo PMA_showMySQLDocu('Error-returns', 'Error-returns');
  511.             
  512.             if (!empty($back_url) && $exit) {
  513.                 echo ' · [<a href="' . $back_url . '">' . $GLOBALS['strBack'] . '</a>]';
  514.             }
  515.             echo "\n";
  516.  
  517.             if ($exit) {
  518.                 include('./footer.inc.php');
  519.                 exit();
  520.             }
  521.         } // end of the 'PMA_mysqlDie()' function
  522.  
  523.  
  524.         /**
  525.          * Defines whether a string exists inside an array or not
  526.          *
  527.          * @param   string   string to search for
  528.          * @param   mixed    array to search into
  529.          *
  530.          * @return  integer  the rank of the $toFind string in the array or '-1' if
  531.          *                   it hasn't been found
  532.          *
  533.          * @access  public
  534.          */
  535.         function PMA_isInto($toFind = '', &$in)
  536.         {
  537.             $max = count($in);
  538.             for ($i = 0; $i < $max && ($toFind != $in[$i]); $i++) {
  539.                 // void();
  540.             }
  541.  
  542.             return ($i < $max) ? $i : -1;
  543.         }  // end of the 'PMA_isInto()' function
  544.     }
  545.  
  546.     /**
  547.      * Determines the font sizes to use depending on the os and browser of the
  548.      * user.
  549.      *
  550.      * This function is based on an article from phpBuilder (see
  551.      * http://www.phpbuilder.net/columns/tim20000821.php).
  552.      *
  553.      * @return  boolean    always true
  554.      *
  555.      * @global  string     the standard font size
  556.      * @global  string     the font size for titles
  557.      * @global  string     the small font size
  558.      * @global  string     the smallest font size
  559.      *
  560.      * @access  public
  561.      *
  562.      * @version 1.1
  563.      */
  564.     function PMA_setFontSizes()
  565.     {
  566.         global $font_size, $font_biggest, $font_bigger, $font_smaller, $font_smallest;
  567.  
  568.         // IE (<6)/Opera (<7) for win case: needs smaller fonts than anyone else
  569.         if (PMA_USR_OS == 'Win'
  570.             && ((PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER < 6)
  571.             || (PMA_USR_BROWSER_AGENT == 'OPERA' && PMA_USR_BROWSER_VER < 7))) {
  572.             $font_size     = 'x-small';
  573.             $font_biggest  = 'large';
  574.             $font_bigger   = 'medium';
  575.             $font_smaller  = '90%';
  576.             $font_smallest = '7pt';
  577.         }
  578.         // IE6 and other browsers for win case
  579.         else if (PMA_USR_OS == 'Win') {
  580.             $font_size     = 'small';
  581.             $font_biggest  = 'large';
  582.             $font_bigger   = 'medium';
  583.             $font_smaller  = (PMA_USR_BROWSER_AGENT == 'IE')
  584.                             ? '90%'
  585.                             : 'x-small';
  586.             $font_smallest = 'x-small';
  587.         }
  588.         // Some mac browsers need also smaller default fonts size (OmniWeb &
  589.         // Opera)...
  590.         else if (PMA_USR_OS == 'Mac'
  591.                     && (PMA_USR_BROWSER_AGENT == 'OMNIWEB' || PMA_USR_BROWSER_AGENT == 'OPERA' || PMA_USR_BROWSER_AGENT == 'SAFARI')) {
  592.             $font_size     = 'x-small';
  593.             $font_biggest  = 'large';
  594.             $font_bigger   = 'medium';
  595.             $font_smaller  = '90%';
  596.             $font_smallest = '7pt';
  597.         }
  598.         // ... but most of them (except IE 5+ & NS 6+) need bigger fonts
  599.         else if ((PMA_USR_OS == 'Mac'
  600.                     && ((PMA_USR_BROWSER_AGENT != 'IE' && PMA_USR_BROWSER_AGENT != 'MOZILLA')
  601.                         || PMA_USR_BROWSER_VER < 5))
  602.                 || PMA_USR_BROWSER_AGENT == 'KONQUEROR') {
  603.             $font_size     = 'medium';
  604.             $font_biggest  = 'x-large';
  605.             $font_bigger   = 'large';
  606.             $font_smaller  = 'small';
  607.             $font_smallest = 'x-small';
  608.         }
  609.         // OS/2 browser
  610.         else if (PMA_USR_OS == 'OS/2'
  611.                     && PMA_USR_BROWSER_AGENT == 'OPERA') {
  612.             $font_size     = 'small';
  613.             $font_biggest  = 'medium';
  614.             $font_bigger   = 'medium';
  615.             $font_smaller  = 'x-small';
  616.             $font_smallest = 'x-small';
  617.         }
  618.         else {
  619.             $font_size     = 'small';
  620.             $font_biggest  = 'large';
  621.             $font_bigger   = 'medium';
  622.             $font_smaller  = 'x-small';
  623.             $font_smallest = 'x-small';
  624.         }
  625.  
  626.         return TRUE;
  627.     } // end of the 'PMA_setFontSizes()' function
  628.  
  629.  
  630.     if ($is_minimum_common == FALSE) {
  631.         /**
  632.          * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
  633.          * set properly and, depending on browsers, inserting or updating a
  634.          * record might fail
  635.          */
  636.         $display_pmaAbsoluteUri_warning = 0;
  637.  
  638.         // Olivier: Setup a default value to let the people and lazy syadmins
  639.         //          work anyway, but display a big warning on the main.php
  640.         //          page.
  641.         if (empty($cfg['PmaAbsoluteUri'])) {
  642.             if (!empty($_SERVER)) {
  643.                 $SERVER_ARRAY = '_SERVER';
  644.             } else if (!empty($HTTP_SERVER_VARS)) {
  645.                 $SERVER_ARRAY = 'HTTP_SERVER_VARS';
  646.             } else {
  647.                 $SERVER_ARRAY = 'GLOBALS';
  648.             } // end if
  649.             if (isset(${$SERVER_ARRAY}['HTTP_HOST'])) {
  650.                 $HTTP_HOST = ${$SERVER_ARRAY}['HTTP_HOST'];
  651.             }
  652.             if (isset(${$SERVER_ARRAY}['HTTPS'])) {
  653.                 $HTTPS = ${$SERVER_ARRAY}['HTTPS'];
  654.             }
  655.             if (isset(${$SERVER_ARRAY}['SERVER_PORT'])) {
  656.                 $SERVER_PORT = ${$SERVER_ARRAY}['SERVER_PORT'];
  657.             }
  658.             if (isset(${$SERVER_ARRAY}['REQUEST_URI'])) {
  659.                 $REQUEST_URI = ${$SERVER_ARRAY}['REQUEST_URI'];
  660.             }
  661.             if (isset(${$SERVER_ARRAY}['PATH_INFO'])) {
  662.                 $PATH_INFO = ${$SERVER_ARRAY}['PATH_INFO'];
  663.             }
  664.             $port_in_HTTP_HOST              = (strpos($HTTP_HOST, ':') > 0);
  665.             $cfg['PmaAbsoluteUri']          = ((!empty($HTTPS) && strtolower($HTTPS) != 'off') ? 'https' : 'http') . '://'
  666.                                             . $HTTP_HOST;
  667.  
  668.             // if $cfg['PmaAbsoluteUri'] is empty and port == 80 or port == 443, do not add ":80" or ":443"
  669.             // to the generated URL -> prevents a double password query in case of http authentication.
  670.  
  671.             if (!(!$port_in_HTTP_HOST && !empty($SERVER_PORT) && ($SERVER_PORT == 80 || $SERVER_PORT == 443))) {
  672.                 $cfg['PmaAbsoluteUri']      .= ((!empty($SERVER_PORT) && !$port_in_HTTP_HOST) ? ':' . $SERVER_PORT : '');
  673.             }
  674.  
  675.             // rabus: if php is in CGI mode, $PHP_SELF often contains the path to the CGI executable.
  676.             //   This is why we try to get the path from $REQUEST_URI or $PATH_INFO first.
  677.             if (isset($REQUEST_URI)) {
  678.                 $cfg['PmaAbsoluteUri']      .= substr($REQUEST_URI, 0, strrpos($REQUEST_URI, '/') + 1);
  679.             } else if (isset($PATH_INFO)) {
  680.                 $cfg['PmaAbsoluteUri']      .= substr($PATH_INFO, 0, strrpos($PATH_INFO, '/') + 1);
  681.             } else {
  682.                 $cfg['PmaAbsoluteUri']      .= substr($PHP_SELF, 0, strrpos($PHP_SELF, '/') + 1);
  683.             }
  684.  
  685.             // We display the warning by default, but not if it is disabled thru
  686.             // via the $cfg['PmaAbsoluteUri_DisableWarning'] variable.
  687.             // This is intended for sysadmins that actually want the default
  688.             // behaviour of auto-detection due to their setup.
  689.             // See the mailing list message:
  690.             // http://sourceforge.net/mailarchive/forum.php?thread_id=859093&forum_id=2141
  691.             if ($cfg['PmaAbsoluteUri_DisableWarning'] == FALSE) {
  692.                 $display_pmaAbsoluteUri_warning = 1;
  693.             }
  694.         }
  695.         // Adds a trailing slash et the end of the phpMyAdmin uri if it does not
  696.         // exist
  697.         else if (substr($cfg['PmaAbsoluteUri'], -1) != '/') {
  698.             $cfg['PmaAbsoluteUri'] .= '/';
  699.         }
  700.  
  701.  
  702.         /**
  703.          * Use mysql_connect() or mysql_pconnect()?
  704.          */
  705.         $connect_func = ($cfg['PersistentConnections']) ? 'mysql_pconnect' : 'mysql_connect';
  706.         $dblist       = array();
  707.  
  708.  
  709.         /**
  710.          * Gets the valid servers list and parameters
  711.          */
  712.         reset($cfg['Servers']);
  713.         while (list($key, $val) = each($cfg['Servers'])) {
  714.             // Don't use servers with no hostname
  715.             if ( ($val['connect_type'] == 'tcp') && empty($val['host']) ) {
  716.                 unset($cfg['Servers'][$key]);
  717.             }
  718.  
  719.             // Final solution to bug #582890
  720.             // If we are using a socket connection
  721.             // and there is nothing in the verbose server name
  722.             // or the host field, then generate a name for the server
  723.             // in the form of "Server 2", localized of course!
  724.             if ( ($val['connect_type'] == 'socket') && empty($val['host']) && empty($val['verbose']) ) {
  725.                 $cfg['Servers'][$key]['verbose'] = sprintf($GLOBALS['strServer'], $key);
  726.                 $val['verbose']                  = sprintf($GLOBALS['strServer'],$key);
  727.             }
  728.         }
  729.  
  730.         if (empty($server) || !isset($cfg['Servers'][$server]) || !is_array($cfg['Servers'][$server])) {
  731.             $server = $cfg['ServerDefault'];
  732.         }
  733.  
  734.  
  735.         /**
  736.          * If no server is selected, make sure that $cfg['Server'] is empty (so
  737.          * that nothing will work), and skip server authentication.
  738.          * We do NOT exit here, but continue on without logging into any server.
  739.          * This way, the welcome page will still come up (with no server info) and
  740.          * present a choice of servers in the case that there are multiple servers
  741.          * and '$cfg['ServerDefault'] = 0' is set.
  742.          */
  743.         if ($server == 0) {
  744.             $cfg['Server'] = array();
  745.         }
  746.  
  747.         /**
  748.          * Otherwise, set up $cfg['Server'] and do the usual login stuff.
  749.          */
  750.         else if (isset($cfg['Servers'][$server])) {
  751.             $cfg['Server'] = $cfg['Servers'][$server];
  752.  
  753.             // Check how the config says to connect to the server
  754.             $server_port   = (empty($cfg['Server']['port']))
  755.                            ? ''
  756.                            : ':' . $cfg['Server']['port'];
  757.             if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
  758.                 $cfg['Server']['socket'] = '';
  759.             }
  760.             $server_socket = (empty($cfg['Server']['socket']) || PMA_PHP_INT_VERSION < 30010)
  761.                            ? ''
  762.                            : ':' . $cfg['Server']['socket'];
  763.             if (PMA_PHP_INT_VERSION >= 40300 && PMA_MYSQL_CLIENT_API >= 32349) {
  764.                 $client_flags = $cfg['Server']['compress'] && defined('MYSQL_CLIENT_COMPRESS') ? MYSQL_CLIENT_COMPRESS : 0;
  765.             }
  766.  
  767.             // Gets the authentication library that fits the $cfg['Server'] settings
  768.             // and run authentication
  769.  
  770.             // (for a quick check of path disclosure in auth/cookies:)
  771.             $coming_from_common = TRUE;
  772.  
  773.             include('./libraries/auth/' . $cfg['Server']['auth_type'] . '.auth.lib.php');
  774.             if (!PMA_auth_check()) {
  775.                 PMA_auth();
  776.             } else {
  777.                 PMA_auth_set_user();
  778.             }
  779.  
  780.             // Check IP-based Allow/Deny rules as soon as possible to reject the
  781.             // user
  782.             // Based on mod_access in Apache:
  783.             // http://cvs.apache.org/viewcvs.cgi/httpd-2.0/modules/aaa/mod_access.c?rev=1.37&content-type=text/vnd.viewcvs-markup
  784.             // Look at: "static int check_dir_access(request_rec *r)"
  785.             // Robbat2 - May 10, 2002
  786.             if (isset($cfg['Server']['AllowDeny']) && isset($cfg['Server']['AllowDeny']['order'])) {
  787.                 include('./libraries/ip_allow_deny.lib.php');
  788.  
  789.                 $allowDeny_forbidden         = FALSE; // default
  790.                 if ($cfg['Server']['AllowDeny']['order'] == 'allow,deny') {
  791.                     $allowDeny_forbidden     = TRUE;
  792.                     if (PMA_allowDeny('allow')) {
  793.                         $allowDeny_forbidden = FALSE;
  794.                     }
  795.                     if (PMA_allowDeny('deny')) {
  796.                         $allowDeny_forbidden = TRUE;
  797.                     }
  798.                 } else if ($cfg['Server']['AllowDeny']['order'] == 'deny,allow') {
  799.                     if (PMA_allowDeny('deny')) {
  800.                         $allowDeny_forbidden = TRUE;
  801.                     }
  802.                     if (PMA_allowDeny('allow')) {
  803.                         $allowDeny_forbidden = FALSE;
  804.                     }
  805.                 } else if ($cfg['Server']['AllowDeny']['order'] == 'explicit') {
  806.                     if (PMA_allowDeny('allow')
  807.                         && !PMA_allowDeny('deny')) {
  808.                         $allowDeny_forbidden = FALSE;
  809.                     } else {
  810.                         $allowDeny_forbidden = TRUE;
  811.                     }
  812.                 } // end if... else if... else if
  813.  
  814.                 // Ejects the user if banished
  815.                 if ($allowDeny_forbidden) {
  816.                    PMA_auth_fails();
  817.                 }
  818.                 unset($allowDeny_forbidden); //Clean up after you!
  819.             } // end if
  820.  
  821.             // The user can work with only some databases
  822.             if (isset($cfg['Server']['only_db']) && $cfg['Server']['only_db'] != '') {
  823.                 if (is_array($cfg['Server']['only_db'])) {
  824.                     $dblist   = $cfg['Server']['only_db'];
  825.                 } else {
  826.                     $dblist[] = $cfg['Server']['only_db'];
  827.                 }
  828.             } // end if
  829.  
  830.             if (PMA_PHP_INT_VERSION >= 40000) {
  831.                 $bkp_track_err = @ini_set('track_errors', 1);
  832.             }
  833.  
  834.             // Try to connect MySQL with the control user profile (will be used to
  835.             // get the privileges list for the current user but the true user link
  836.             // must be open after this one so it would be default one for all the
  837.             // scripts)
  838.             if ($cfg['Server']['controluser'] != '') {
  839.                 if (empty($client_flags)) {
  840.                     $dbh            = @$connect_func(
  841.                                           $cfg['Server']['host'] . $server_port . $server_socket,
  842.                                           $cfg['Server']['controluser'],
  843.                                           $cfg['Server']['controlpass']
  844.                                       );
  845.                 } else {
  846.                     $dbh            = @$connect_func(
  847.                                           $cfg['Server']['host'] . $server_port . $server_socket,
  848.                                           $cfg['Server']['controluser'],
  849.                                           $cfg['Server']['controlpass'],
  850.                                           FALSE,
  851.                                           $client_flags
  852.                                       );
  853.                 }
  854.                 if ($dbh == FALSE) {
  855.                     if (PMA_mysql_error()) {
  856.                         $conn_error = PMA_mysql_error();
  857.                     } else if (isset($php_errormsg)) {
  858.                         $conn_error = $php_errormsg;
  859.                     } else {
  860.                         $conn_error = 'Cannot connect: invalid settings.';
  861.                     }
  862.                     $local_query    = $connect_func . '('
  863.                                     . $cfg['Server']['host'] . $server_port . $server_socket . ', '
  864.                                     . $cfg['Server']['controluser'] . ', '
  865.                                     . $cfg['Server']['controlpass']
  866.                                     . (empty($client_flags) ? '' : ', FALSE, ' . $client_flags)
  867.                                     . ')';
  868.                     if (empty($GLOBALS['is_header_sent'])) {
  869.                         include('./header.inc.php');
  870.                     }
  871.                     //PMA_mysqlDie($conn_error, $local_query, FALSE);
  872.                     PMA_mysqlDie($conn_error, '', FALSE);
  873.                 } // end if
  874.             } // end if
  875.  
  876.             // Pass #1 of DB-Config to read in master level DB-Config will go here
  877.             // Robbat2 - May 11, 2002
  878.  
  879.             // Connects to the server (validates user's login)
  880.             if (empty($client_flags)) {
  881.                 $userlink           = @$connect_func(
  882.                                           $cfg['Server']['host'] . $server_port . $server_socket,
  883.                                           $cfg['Server']['user'],
  884.                                           $cfg['Server']['password']
  885.                                       );
  886.             } else {
  887.                 $userlink           = @$connect_func(
  888.                                           $cfg['Server']['host'] . $server_port . $server_socket,
  889.                                           $cfg['Server']['user'],
  890.                                           $cfg['Server']['password'],
  891.                                           FALSE,
  892.                                           $client_flags
  893.                                       );
  894.             }
  895.             if ($userlink == FALSE) {
  896.                 PMA_auth_fails();
  897.             } // end if
  898.  
  899.             // Pass #2 of DB-Config to read in user level DB-Config will go here
  900.             // Robbat2 - May 11, 2002
  901.  
  902.             if (PMA_PHP_INT_VERSION >= 40000) {
  903.                 @ini_set('track_errors', $bkp_track_err);
  904.             }
  905.  
  906.             // If controluser isn't defined, use the current user settings to get
  907.             // his rights
  908.             if ($cfg['Server']['controluser'] == '') {
  909.                 $dbh = $userlink;
  910.             }
  911.  
  912.             // Runs the "defines.lib.php" for the second time to get the mysql
  913.             // release number
  914.             include('./libraries/defines.lib.php');
  915.  
  916.             // if 'only_db' is set for the current user, there is no need to check for
  917.             // available databases in the "mysql" db
  918.             $dblist_cnt = count($dblist);
  919.             if ($dblist_cnt) {
  920.                 $true_dblist  = array();
  921.                 $is_show_dbs  = TRUE;
  922.                 for ($i = 0; $i < $dblist_cnt; $i++) {
  923.                     if ($is_show_dbs && ereg('(^|[^\])(_|%)', $dblist[$i])) {
  924.                         $local_query = 'SHOW DATABASES LIKE \'' . $dblist[$i] . '\'';
  925.                         $rs          = PMA_mysql_query($local_query, $dbh);
  926.                         // "SHOW DATABASES" statement is disabled
  927.                         if ($i == 0
  928.                             && (PMA_mysql_error() && mysql_errno() == 1045)) {
  929.                             $true_dblist[] = str_replace('\\_', '_', str_replace('\\%', '%', $dblist[$i]));
  930.                             $is_show_dbs   = FALSE;
  931.                         }
  932.                         // Debug
  933.                         // else if (PMA_mysql_error()) {
  934.                         //    PMA_mysqlDie('', $local_query, FALSE);
  935.                         // }
  936.                         while ($row = @PMA_mysql_fetch_row($rs)) {
  937.                             $true_dblist[] = $row[0];
  938.                         } // end while
  939.                         if ($rs) {
  940.                             mysql_free_result($rs);
  941.                         }
  942.                     } else {
  943.                         $true_dblist[]     = str_replace('\\_', '_', str_replace('\\%', '%', $dblist[$i]));
  944.                     } // end if... else...
  945.                 } // end for
  946.                 $dblist       = $true_dblist;
  947.                 unset($true_dblist);
  948.             } // end if
  949.  
  950.             // 'only_db' is empty for the current user...
  951.             else {
  952.                 // ... first checks whether the "safe_show_database" is on or not
  953.                 //     (if MYSQL supports this)
  954.                 if (PMA_MYSQL_INT_VERSION >= 32330) {
  955.                     $is_safe_show_dbs = FALSE;
  956.                     if (PMA_MYSQL_INT_VERSION >= 40002) {
  957.                         $is_safe_show_dbs = 'ON';
  958.                     }
  959.                     else {
  960.                         $local_query      = 'SHOW VARIABLES LIKE \'safe\\_show\\_database\'';
  961.                         $rs               = PMA_mysql_query($local_query, $dbh); // Debug: or PMA_mysqlDie('', $local_query, FALSE);
  962.                         $is_safe_show_dbs = ($rs) ? @PMA_mysql_result($rs, 0, 'Value') : FALSE;
  963.                         mysql_free_result($rs);
  964.                     }
  965.                           // ... and if on, try to get the available dbs list
  966.                     if ($is_safe_show_dbs && strtoupper($is_safe_show_dbs) != 'OFF') {
  967.                         $uva_alldbs   = mysql_list_dbs($userlink);
  968.                         while ($uva_row = PMA_mysql_fetch_array($uva_alldbs)) {
  969.                               $dblist[] = $uva_row[0];
  970.                         } // end while
  971.                         $dblist_cnt   = count($dblist);
  972.                         unset($uva_alldbs);
  973.                     } // end if ($is_safe_show_dbs)
  974.                 } //end if (PMA_MYSQL_INT_VERSION)
  975.  
  976.                 // ... else checks for available databases in the "mysql" db
  977.                 if (!$dblist_cnt) {
  978.                     $auth_query   = 'SELECT User, Select_priv '
  979.                                   . 'FROM mysql.user '
  980.                                   . 'WHERE User = \'' . PMA_sqlAddslashes($cfg['Server']['user']) . '\'';
  981.                     $rs           = PMA_mysql_query($auth_query, $dbh); // Debug: or PMA_mysqlDie('', $auth_query, FALSE);
  982.                 } // end
  983.             } // end if (!$dblist_cnt)
  984.  
  985.             // Access to "mysql" db allowed and dblist still empty -> gets the
  986.             // usable db list
  987.             if (!$dblist_cnt
  988.                 && ($rs && @mysql_numrows($rs))) {
  989.                 $row = PMA_mysql_fetch_array($rs);
  990.                 mysql_free_result($rs);
  991.                 // Correction uva 19991215
  992.                 // Previous code assumed database "mysql" admin table "db" column
  993.                 // "db" contains literal name of user database, and works if so.
  994.                 // Mysql usage generally (and uva usage specifically) allows this
  995.                 // column to contain regular expressions (we have all databases
  996.                 // owned by a given student/faculty/staff beginning with user i.d.
  997.                 // and governed by default by a single set of privileges with
  998.                 // regular expression as key). This breaks previous code.
  999.                 // This maintenance is to fix code to work correctly for regular
  1000.                 // expressions.
  1001.                 if ($row['Select_priv'] != 'Y') {
  1002.  
  1003.                     // 1. get allowed dbs from the "mysql.db" table
  1004.                     // lem9: User can be blank (anonymous user)
  1005.                     $local_query = 'SELECT DISTINCT Db FROM mysql.db WHERE Select_priv = \'Y\' AND (User = \'' . PMA_sqlAddslashes($cfg['Server']['user']) . '\' OR User = \'\')';
  1006.                     $rs          = PMA_mysql_query($local_query, $dbh); // Debug: or PMA_mysqlDie('', $local_query, FALSE);
  1007.                     if ($rs && @mysql_numrows($rs)) {
  1008.                         // Will use as associative array of the following 2 code
  1009.                         // lines:
  1010.                         //   the 1st is the only line intact from before
  1011.                         //     correction,
  1012.                         //   the 2nd replaces $dblist[] = $row['Db'];
  1013.                         $uva_mydbs = array();
  1014.                         // Code following those 2 lines in correction continues
  1015.                         // populating $dblist[], as previous code did. But it is
  1016.                         // now populated with actual database names instead of
  1017.                         // with regular expressions.
  1018.                         while ($row = PMA_mysql_fetch_array($rs)) {
  1019.                             // loic1: all databases cases - part 1
  1020.                             if (empty($row['Db']) || $row['Db'] == '%') {
  1021.                                 $uva_mydbs['%'] = 1;
  1022.                                 break;
  1023.                             }
  1024.                             // loic1: avoid multiple entries for dbs
  1025.                             if (!isset($uva_mydbs[$row['Db']])) {
  1026.                                 $uva_mydbs[$row['Db']] = 1;
  1027.                             }
  1028.                         } // end while
  1029.                         mysql_free_result($rs);
  1030.                         $uva_alldbs = mysql_list_dbs($dbh);
  1031.                         // loic1: all databases cases - part 2
  1032.                         if (isset($uva_mydbs['%'])) {
  1033.                             while ($uva_row = PMA_mysql_fetch_array($uva_alldbs)) {
  1034.                                 $dblist[] = $uva_row[0];
  1035.                             } // end while
  1036.                         } // end if
  1037.                         else {
  1038.                             while ($uva_row = PMA_mysql_fetch_array($uva_alldbs)) {
  1039.                                 $uva_db = $uva_row[0];
  1040.                                 if (isset($uva_mydbs[$uva_db]) && $uva_mydbs[$uva_db] == 1) {
  1041.                                     $dblist[]           = $uva_db;
  1042.                                     $uva_mydbs[$uva_db] = 0;
  1043.                                 } else if (!isset($dblist[$uva_db])) {
  1044.                                     reset($uva_mydbs);
  1045.                                     while (list($uva_matchpattern, $uva_value) = each($uva_mydbs)) {
  1046.                                         // loic1: fixed bad regexp
  1047.                                         // TODO: db names may contain characters
  1048.                                         //       that are regexp instructions
  1049.                                         $re        = '(^|(\\\\\\\\)+|[^\])';
  1050.                                         $uva_regex = ereg_replace($re . '%', '\\1.*', ereg_replace($re . '_', '\\1.{1}', $uva_matchpattern));
  1051.                                         // Fixed db name matching
  1052.                                         // 2000-08-28 -- Benjamin Gandon
  1053.                                         if (ereg('^' . $uva_regex . '$', $uva_db)) {
  1054.                                             $dblist[] = $uva_db;
  1055.                                             break;
  1056.                                         }
  1057.                                     } // end while
  1058.                                 } // end if ... else if....
  1059.                             } // end while
  1060.                         } // end else
  1061.                         mysql_free_result($uva_alldbs);
  1062.                         unset($uva_mydbs);
  1063.                     } // end if
  1064.  
  1065.                     // 2. get allowed dbs from the "mysql.tables_priv" table
  1066.                     $local_query = 'SELECT DISTINCT Db FROM mysql.tables_priv WHERE Table_priv LIKE \'%Select%\' AND User = \'' . PMA_sqlAddslashes($cfg['Server']['user']) . '\'';
  1067.                     $rs          = PMA_mysql_query($local_query, $dbh); // Debug: or PMA_mysqlDie('', $local_query, FALSE);
  1068.                     if ($rs && @mysql_numrows($rs)) {
  1069.                         while ($row = PMA_mysql_fetch_array($rs)) {
  1070.                             if (PMA_isInto($row['Db'], $dblist) == -1) {
  1071.                                 $dblist[] = $row['Db'];
  1072.                             }
  1073.                         } // end while
  1074.                         mysql_free_result($rs);
  1075.                     } // end if
  1076.                 } // end if
  1077.             } // end building available dbs from the "mysql" db
  1078.  
  1079.         } // end server connecting
  1080.         /**
  1081.          * Missing server hostname
  1082.          */
  1083.         else {
  1084.             echo $strHostEmpty;
  1085.         }
  1086.  
  1087.  
  1088.         /**
  1089.          * Get the list and number of available databases.
  1090.          *
  1091.          * @param   string   the url to go back to in case of error
  1092.          *
  1093.          * @return  boolean  always true
  1094.          *
  1095.          * @global  array    the list of available databases
  1096.          * @global  integer  the number of available databases
  1097.          */
  1098.         function PMA_availableDatabases($error_url = '')
  1099.         {
  1100.             global $dblist;
  1101.             global $num_dbs;
  1102.  
  1103.             $num_dbs = count($dblist);
  1104.  
  1105.             // 1. A list of allowed databases has already been defined by the
  1106.             //    authentification process -> gets the available databases list
  1107.             if ($num_dbs) {
  1108.                 $true_dblist = array();
  1109.                 for ($i = 0; $i < $num_dbs; $i++) {
  1110.                     $dblink  = @PMA_mysql_select_db($dblist[$i]);
  1111.                     if ($dblink) {
  1112.                         $true_dblist[] = $dblist[$i];
  1113.                     } // end if
  1114.                 } // end for
  1115.                 $dblist      = array();
  1116.                 $dblist      = $true_dblist;
  1117.                 unset($true_dblist);
  1118.                 $num_dbs     = count($dblist);
  1119.             } // end if
  1120.  
  1121.             // 2. Allowed database list is empty -> gets the list of all databases
  1122.             //    on the server
  1123.             else {
  1124.                 $dbs          = mysql_list_dbs() or PMA_mysqlDie('', 'SHOW DATABASES;', FALSE, $error_url);
  1125.                 $num_dbs      = ($dbs) ? @mysql_num_rows($dbs) : 0;
  1126.                 $real_num_dbs = 0;
  1127.                 for ($i = 0; $i < $num_dbs; $i++) {
  1128.                     $db_name_tmp = PMA_mysql_dbname($dbs, $i);
  1129.                     $dblink      = @PMA_mysql_select_db($db_name_tmp);
  1130.                     if ($dblink) {
  1131.                         $dblist[] = $db_name_tmp;
  1132.                         $real_num_dbs++;
  1133.                     }
  1134.                 } // end for
  1135.                 mysql_free_result($dbs);
  1136.                 $num_dbs = $real_num_dbs;
  1137.             } // end else
  1138.  
  1139.             return TRUE;
  1140.         } // end of the 'PMA_availableDatabases()' function
  1141.  
  1142.  
  1143.  
  1144.         /* ----------------------- Set of misc functions ----------------------- */
  1145.  
  1146.  
  1147.         /**
  1148.          * Adds backquotes on both sides of a database, table or field name.
  1149.          * Since MySQL 3.23.6 this allows to use non-alphanumeric characters in
  1150.          * these names.
  1151.          *
  1152.          * @param   mixed    the database, table or field name to "backquote" or
  1153.          *                   array of it
  1154.          * @param   boolean  a flag to bypass this function (used by dump
  1155.          *                   functions)
  1156.          *
  1157.          * @return  mixed    the "backquoted" database, table or field name if the
  1158.          *                   current MySQL release is >= 3.23.6, the original one
  1159.          *                   else
  1160.          *
  1161.          * @access  public
  1162.          */
  1163.         function PMA_backquote($a_name, $do_it = TRUE)
  1164.         {
  1165.             if ($do_it
  1166.                 && PMA_MYSQL_INT_VERSION >= 32306
  1167.                 && !empty($a_name) && $a_name != '*') {
  1168.  
  1169.                 if (is_array($a_name)) {
  1170.                      $result = array();
  1171.                      reset($a_name);
  1172.                      while(list($key, $val) = each($a_name)) {
  1173.                          $result[$key] = '`' . $val . '`';
  1174.                      }
  1175.                      return $result;
  1176.                 } else {
  1177.                     return '`' . $a_name . '`';
  1178.                 }
  1179.             } else {
  1180.                 return $a_name;
  1181.             }
  1182.         } // end of the 'PMA_backquote()' function
  1183.  
  1184.  
  1185.         /**
  1186.          * Format a string so it can be passed to a javascript function.
  1187.          * This function is used to displays a javascript confirmation box for
  1188.          * "DROP/DELETE/ALTER" queries.
  1189.          *
  1190.          * @param   string   the string to format
  1191.          * @param   boolean  whether to add backquotes to the string or not
  1192.          *
  1193.          * @return  string   the formated string
  1194.          *
  1195.          * @access  public
  1196.          */
  1197.         function PMA_jsFormat($a_string = '', $add_backquotes = TRUE)
  1198.         {
  1199.             if (is_string($a_string)) {
  1200.                 $a_string = htmlspecialchars($a_string);
  1201.                 $a_string = str_replace('\\', '\\\\', $a_string);
  1202.                 $a_string = str_replace('\'', '\\\'', $a_string);
  1203.                 $a_string = str_replace('#', '\\#', $a_string);
  1204.                 $a_string = str_replace("\012", '\\\\n', $a_string);
  1205.                 $a_string = str_replace("\015", '\\\\r', $a_string);
  1206.             }
  1207.  
  1208.             return (($add_backquotes) ? PMA_backquote($a_string) : $a_string);
  1209.         } // end of the 'PMA_jsFormat()' function
  1210.  
  1211.  
  1212.         /**
  1213.          * Defines the <CR><LF> value depending on the user OS.
  1214.          *
  1215.          * @return  string   the <CR><LF> value to use
  1216.          *
  1217.          * @access  public
  1218.          */
  1219.         function PMA_whichCrlf()
  1220.         {
  1221.             $the_crlf = "\n";
  1222.  
  1223.             // The 'PMA_USR_OS' constant is defined in "./libraries/defines.lib.php"
  1224.             // Win case
  1225.             if (PMA_USR_OS == 'Win') {
  1226.                 $the_crlf = "\r\n";
  1227.             }
  1228.             // Mac case
  1229.             else if (PMA_USR_OS == 'Mac') {
  1230.                 $the_crlf = "\r";
  1231.             }
  1232.             // Others
  1233.             else {
  1234.                 $the_crlf = "\n";
  1235.             }
  1236.  
  1237.             return $the_crlf;
  1238.         } // end of the 'PMA_whichCrlf()' function
  1239.  
  1240.  
  1241.         /**
  1242.          * Counts and displays the number of records in a table
  1243.          *
  1244.          * Last revision 13 July 2001: Patch for limiting dump size from
  1245.          * vinay@sanisoft.com & girish@sanisoft.com
  1246.          *
  1247.          * @param   string   the current database name
  1248.          * @param   string   the current table name
  1249.          * @param   boolean  whether to retain or to displays the result
  1250.          *
  1251.          * @return  mixed    the number of records if retain is required, true else
  1252.          *
  1253.          * @access  public
  1254.          */
  1255.         function PMA_countRecords($db, $table, $ret = FALSE)
  1256.         {
  1257.             global $err_url, $cfg;
  1258.             if (PMA_MYSQL_INT_VERSION >= 32303) {
  1259.                 $local_query  = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'';
  1260.                 $result       = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
  1261.                 $showtable    = PMA_mysql_fetch_array($result);
  1262.                 $num     = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
  1263.                 if ($num < $cfg['MaxExactCount']) unset($num);
  1264.                 mysql_free_result($result);
  1265.             }
  1266.             if (!isset($num)) {
  1267.                 $result = PMA_mysql_query('SELECT COUNT(*) AS num FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table)) or PMA_mysqlDie('', $local_query, '', $err_url);
  1268.                 $num    = ($result) ? PMA_mysql_result($result, 0, 'num') : 0;
  1269.                 mysql_free_result($result);
  1270.             }
  1271.             if ($ret) {
  1272.                 return $num;
  1273.             } else {
  1274.                 echo number_format($num, 0, $GLOBALS['number_decimal_separator'], $GLOBALS['number_thousands_separator']);
  1275.                 return TRUE;
  1276.             }
  1277.         } // end of the 'PMA_countRecords()' function
  1278.  
  1279.  
  1280.         /**
  1281.          * Displays a message at the top of the "main" (right) frame
  1282.          *
  1283.          * @param   string  the message to display
  1284.          *
  1285.          * @global  array   the configuration array
  1286.          *
  1287.          * @access  public
  1288.          */
  1289.         function PMA_showMessage($message)
  1290.         {
  1291.             global $cfg;
  1292.  
  1293.             // Reloads the navigation frame via JavaScript if required
  1294.             if (isset($GLOBALS['reload']) && $GLOBALS['reload']) {
  1295.                 echo "\n";
  1296.                 $reload_url = './left.php?' . PMA_generate_common_url((isset($GLOBALS['db']) ? $GLOBALS['db'] : ''), '', '&')
  1297.                 ?>
  1298.     <script type="text/javascript" language="javascript1.2">
  1299.     <!--
  1300.     if (typeof(window.parent) != 'undefined'
  1301.         && typeof(window.parent.frames['nav']) != 'undefined') {
  1302.         window.parent.frames['nav'].location.replace('<?php echo $reload_url; ?>&hash=' + <?php echo (($cfg['QueryFrame'] && $cfg['QueryFrameJS']) ? 'window.parent.frames[\'queryframe\'].document.hashform.hash.value' : "'" . md5($cfg['PmaAbsoluteUri']) . "'"); ?>);
  1303.     }
  1304.     //-->
  1305.     </script>
  1306.                 <?php
  1307.                 unset($GLOBALS['reload']);
  1308.             }
  1309.  
  1310.             // Corrects the tooltip text via JS if required
  1311.             else if (!empty($GLOBALS['table']) && $cfg['ShowTooltip'] && PMA_MYSQL_INT_VERSION >= 32303) {
  1312.                 $result = @PMA_mysql_query('SHOW TABLE STATUS FROM ' . PMA_backquote($GLOBALS['db']) . ' LIKE \'' . PMA_sqlAddslashes($GLOBALS['table'], TRUE) . '\'');
  1313.                 if ($result) {
  1314.                     $tbl_status = PMA_mysql_fetch_array($result, MYSQL_ASSOC);
  1315.                     $tooltip    = (empty($tbl_status['Comment']))
  1316.                                 ? ''
  1317.                                 : $tbl_status['Comment'] . ' ';
  1318.                     $tooltip .= '(' . $tbl_status['Rows'] . ' ' . $GLOBALS['strRows'] . ')';
  1319.                     mysql_free_result($result);
  1320.                     $md5_tbl = md5($GLOBALS['table']);
  1321.                     echo "\n";
  1322.                     ?>
  1323.     <script type="text/javascript" language="javascript1.2">
  1324.     <!--
  1325.     if (typeof(document.getElementById) != 'undefined'
  1326.         && typeof(window.parent.frames['nav']) != 'undefined'
  1327.         && typeof(window.parent.frames['nav'].document) != 'undefined' && typeof(window.parent.frames['nav'].document) != 'unknown'
  1328.         && (window.parent.frames['nav'].document.getElementById('<?php echo 'tbl_' . $md5_tbl; ?>'))
  1329.         && typeof(window.parent.frames['nav'].document.getElementById('<?php echo 'tbl_' . $md5_tbl; ?>')) != 'undefined'
  1330.         && typeof(window.parent.frames['nav'].document.getElementById('<?php echo 'tbl_' . $md5_tbl; ?>').title) == 'string') {
  1331.         window.parent.frames['nav'].document.getElementById('<?php echo 'tbl_' . $md5_tbl; ?>').title = '<?php echo PMA_jsFormat($tooltip, FALSE); ?>';
  1332.     }
  1333.     //-->
  1334.     </script>
  1335.                     <?php
  1336.                 } // end if
  1337.             } // end if... else if
  1338.  
  1339.             // Checks if the table needs to be repaired after a TRUNCATE query.
  1340.             if (PMA_MYSQL_INT_VERSION >= 40000
  1341.                 && isset($GLOBALS['table']) && isset($GLOBALS['sql_query'])
  1342.                 && $GLOBALS['sql_query'] == 'TRUNCATE TABLE ' . PMA_backquote($GLOBALS['table'])) {
  1343.                 if (!isset($tbl_status)) {
  1344.                     $result = @PMA_mysql_query('SHOW TABLE STATUS FROM ' . PMA_backquote($GLOBALS['db']) . ' LIKE \'' . PMA_sqlAddslashes($GLOBALS['table'], TRUE) . '\'');
  1345.                     if ($result) {
  1346.                         $tbl_status = PMA_mysql_fetch_array($result, MYSQL_ASSOC);
  1347.                         mysql_free_result($result);
  1348.                     }
  1349.                 }
  1350.                 if (isset($tbl_status) && (int) $tbl_status['Index_length'] > 1024) {
  1351.                     @PMA_mysql_query('REPAIR TABLE ' . PMA_backquote($GLOBALS['table']));
  1352.                 }
  1353.             }
  1354.             unset($tbl_status);
  1355.  
  1356.             echo "\n";
  1357.             ?>
  1358.     <div align="<?php echo $GLOBALS['cell_align_left']; ?>">
  1359.         <table border="<?php echo $cfg['Border']; ?>" cellpadding="5">
  1360.         <tr>
  1361.             <td bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
  1362.                 <b><?php echo $message; ?></b><br />
  1363.             </td>
  1364.         </tr>
  1365.             <?php
  1366.             if ($cfg['ShowSQL'] == TRUE && (!empty($GLOBALS['sql_query']) || !empty($GLOBALS['display_query']))) {
  1367.                 $local_query = !empty($GLOBALS['display_query']) ? $GLOBALS['display_query'] : (($cfg['SQP']['fmtType'] == 'none' && $GLOBALS['unparsed_sql'] != '') ? $GLOBALS['unparsed_sql'] : $GLOBALS['sql_query']);
  1368.                 // Basic url query part
  1369.                 $url_qpart = '?' . PMA_generate_common_url(isset($GLOBALS['db']) ? $GLOBALS['db'] : '', isset($GLOBALS['table']) ? $GLOBALS['table'] : '');
  1370.                 echo "\n";
  1371.                 ?>
  1372.         <tr>
  1373.             <td bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
  1374.                 <?php
  1375.                 echo "\n";
  1376.                 // Html format the query to be displayed
  1377.                 // The nl2br function isn't used because its result isn't a valid
  1378.                 // xhtml1.0 statement before php4.0.5 ("<br>" and not "<br />")
  1379.                 // If we want to show some sql code it is easiest to create it here
  1380.                  /* SQL-Parser-Analyzer */
  1381.                 $sqlnr = 1;
  1382.                 if (!empty($GLOBALS['show_as_php'])) {
  1383.                     $new_line = '\'<br />' . "\n" . '        . \' ';
  1384.                 }
  1385.                 if (isset($new_line)) {
  1386.                      /* SQL-Parser-Analyzer */
  1387.                     $query_base = PMA_sqlAddslashes(htmlspecialchars($local_query));
  1388.                      /* SQL-Parser-Analyzer */
  1389.                     $query_base = ereg_replace("((\015\012)|(\015)|(\012))+", $new_line, $query_base);
  1390.                 } else {
  1391.                     $query_base = $local_query;
  1392.                 }
  1393.                 if (!empty($GLOBALS['show_as_php'])) {
  1394.                     $query_base = '$sql  = \'' . $query_base;
  1395.                 } else if (!empty($GLOBALS['validatequery'])) {
  1396.                     $query_base = PMA_validateSQL($query_base);
  1397.                 } else {
  1398.                     $parsed_sql = PMA_SQP_parse($query_base);
  1399.                     $query_base = PMA_formatSql($parsed_sql, $query_base);
  1400.                 }
  1401.  
  1402.                 // Prepares links that may be displayed to edit/explain the query
  1403.                 // (don't go to default pages, we must go to the page
  1404.                 // where the query box is available)
  1405.                 // (also, I don't see why we should check the goto variable)
  1406.  
  1407.                 //if (!isset($GLOBALS['goto'])) {
  1408.                     //$edit_target = (isset($GLOBALS['table'])) ? $cfg['DefaultTabTable'] : $cfg['DefaultTabDatabase'];
  1409.                 $edit_target = isset($GLOBALS['db']) ? (isset($GLOBALS['table']) ? 'tbl_properties.php' : 'db_details.php') : '';
  1410.                 //} else if ($GLOBALS['goto'] != 'main.php') {
  1411.                 //    $edit_target = $GLOBALS['goto'];
  1412.                 //} else {
  1413.                 //    $edit_target = '';
  1414.                 //}
  1415.  
  1416.                 if (isset($cfg['SQLQuery']['Edit'])
  1417.                     && ($cfg['SQLQuery']['Edit'] == TRUE )
  1418.                     && (!empty($edit_target))) {
  1419.  
  1420.                     $onclick = '';
  1421.                     if ($cfg['QueryFrameJS'] && $cfg['QueryFrame']) {
  1422.                         $onclick = 'onclick="focus_querywindow(\'' . urlencode($local_query) . '\'); return false;"';
  1423.                     }
  1424.  
  1425.                     $edit_link = ' [<a href="'
  1426.                                . $edit_target
  1427.                                . $url_qpart
  1428.                                . '&sql_query=' . urlencode($local_query) . '&show_query=1#querybox" ' . $onclick . '>' . $GLOBALS['strEdit'] . '</a>]';
  1429.                 } else {
  1430.                     $edit_link = '';
  1431.                 }
  1432.  
  1433.                 // Want to have the query explained (Mike Beck 2002-05-22)
  1434.                 // but only explain a SELECT (that has not been explained)
  1435.                 /* SQL-Parser-Analyzer */
  1436.                 if (isset($cfg['SQLQuery']['Explain'])
  1437.                     && $cfg['SQLQuery']['Explain'] == TRUE) {
  1438.  
  1439.                     // Detect if we are validating as well
  1440.                     // To preserve the validate uRL data
  1441.                     if (!empty($GLOBALS['validatequery'])) {
  1442.                         $explain_link_validate = '&validatequery=1';
  1443.                     } else {
  1444.                         $explain_link_validate = '';
  1445.                     }
  1446.  
  1447.                     $explain_link = ' [<a href="sql.php'
  1448.                                   . $url_qpart
  1449.                                   . $explain_link_validate
  1450.                                   . '&sql_query=';
  1451.  
  1452.                     if (eregi('^SELECT[[:space:]]+', $local_query)) {
  1453.                         $explain_link .= urlencode('EXPLAIN ' . $local_query) . '">' . $GLOBALS['strExplain'];
  1454.                     } else if (eregi('^EXPLAIN[[:space:]]+SELECT[[:space:]]+', $local_query)) {
  1455.                         $explain_link .= urlencode(substr($local_query, 8)) . '">' . $GLOBALS['strNoExplain'];
  1456.                     } else {
  1457.                         $explain_link = '';
  1458.                     }
  1459.                     if(!empty($explain_link)) {
  1460.                         $explain_link .= '</a>]';
  1461.                     }
  1462.                 } else {
  1463.                     $explain_link = '';
  1464.                 } //show explain
  1465.  
  1466.                 // Also we would like to get the SQL formed in some nice
  1467.                 // php-code (Mike Beck 2002-05-22)
  1468.                 if (isset($cfg['SQLQuery']['ShowAsPHP'])
  1469.                     && $cfg['SQLQuery']['ShowAsPHP'] == TRUE) {
  1470.                     $php_link = ' [<a href="sql.php'
  1471.                               . $url_qpart
  1472.                               . '&show_query=1'
  1473.                               . '&sql_query=' . urlencode($local_query)
  1474.                               . '&show_as_php=';
  1475.  
  1476.                     if (!empty($GLOBALS['show_as_php'])) {
  1477.                         $php_link .= '0">' . $GLOBALS['strNoPhp'];
  1478.                     } else {
  1479.                         $php_link .= '1">' . $GLOBALS['strPhp'];
  1480.                     }
  1481.                     $php_link .= '</a>]';
  1482.  
  1483.                     if (isset($GLOBALS['show_as_php']) && $GLOBALS['show_as_php'] == '1') {
  1484.                         $php_link .= ' [<a href="sql.php'
  1485.                                   . $url_qpart
  1486.                                   . '&show_query=1'
  1487.                                   . '&sql_query=' . urlencode($local_query)
  1488.                                   . '">' . $GLOBALS['strRunQuery'] . '</a>]';
  1489.                     }
  1490.  
  1491.                 } else {
  1492.                     $php_link = '';
  1493.                 } //show as php
  1494.  
  1495.                 if (isset($cfg['SQLValidator']['use'])
  1496.                     && $cfg['SQLValidator']['use'] == TRUE
  1497.                     && isset($cfg['SQLQuery']['Validate'])
  1498.                     && $cfg['SQLQuery']['Validate'] == TRUE) {
  1499.                     $validate_link = ' [<a href="sql.php'
  1500.                                    . $url_qpart
  1501.                                    . '&show_query=1'
  1502.                                    . '&sql_query=' . urlencode($local_query)
  1503.                                    . '&validatequery=';
  1504.                     if (!empty($GLOBALS['validatequery'])) {
  1505.                         $validate_link .= '0">' .  $GLOBALS['strNoValidateSQL'] ;
  1506.                     } else {
  1507.                         $validate_link .= '1">'. $GLOBALS['strValidateSQL'] ;
  1508.                     }
  1509.                     $validate_link .= '</a>]';
  1510.                 } else {
  1511.                     $validate_link = '';
  1512.                 } //validator
  1513.  
  1514.                 // Displays the message
  1515.                 echo '            ' . $GLOBALS['strSQLQuery'] . ' :';
  1516.                 if (!empty($edit_target)) {
  1517.                     echo $edit_link . $explain_link . $php_link . $validate_link;
  1518.                 }
  1519.                 echo '<br />' . "\n";
  1520.                 echo '            ' . $query_base;
  1521.                 // If a 'LIMIT' clause has been programatically added to the query
  1522.                 // displays it
  1523.                 if (!empty($GLOBALS['sql_limit_to_append'])) {
  1524.                     if (!empty($GLOBALS['show_as_php'])) {
  1525.                         echo $GLOBALS['sql_limit_to_append'];
  1526.                     } else if (!empty($GLOBALS['validatequery'])) {
  1527.                         // skip the extra bit here
  1528.                     } else {
  1529.                         echo ' ' . PMA_formatSql(PMA_SQP_parse($GLOBALS['sql_limit_to_append'], $GLOBALS['sql_limit_to_append']));
  1530.                     }
  1531.                 }
  1532.  
  1533.                 unset($local_query);
  1534.                 //Clean up the end of the PHP
  1535.                 if (!empty($GLOBALS['show_as_php'])) {
  1536.                     echo '\';';
  1537.                 }
  1538.                 echo "\n";
  1539.                 ?>
  1540.             </td>
  1541.         </tr>
  1542.                <?php
  1543.             }
  1544.             echo "\n";
  1545.             ?>
  1546.         </table>
  1547.     </div><br />
  1548.             <?php
  1549.         } // end of the 'PMA_showMessage()' function
  1550.  
  1551.  
  1552.         /**
  1553.          * Formats $value to byte view
  1554.          *
  1555.          * @param    double   the value to format
  1556.          * @param    integer  the sensitiveness
  1557.          * @param    integer  the number of decimals to retain
  1558.          *
  1559.          * @return   array    the formatted value and its unit
  1560.          *
  1561.          * @access  public
  1562.          *
  1563.          * @author   staybyte
  1564.          * @version  1.2 - 18 July 2002
  1565.          */
  1566.         function PMA_formatByteDown($value, $limes = 6, $comma = 0)
  1567.         {
  1568.             $dh           = pow(10, $comma);
  1569.             $li           = pow(10, $limes);
  1570.             $return_value = $value;
  1571.             $unit         = $GLOBALS['byteUnits'][0];
  1572.  
  1573.             for ( $d = 6, $ex = 15; $d >= 1; $d--, $ex-=3 ) {
  1574.                 if (isset($GLOBALS['byteUnits'][$d]) && $value >= $li * pow(10, $ex)) {
  1575.                     $value = round($value / ( pow(1024, $d) / $dh) ) /$dh;
  1576.                     $unit = $GLOBALS['byteUnits'][$d];
  1577.                     break 1;
  1578.                 } // end if
  1579.             } // end for
  1580.  
  1581.             if ($unit != $GLOBALS['byteUnits'][0]) {
  1582.                 $return_value = number_format($value, $comma, $GLOBALS['number_decimal_separator'], $GLOBALS['number_thousands_separator']);
  1583.             } else {
  1584.                 $return_value = number_format($value, 0, $GLOBALS['number_decimal_separator'], $GLOBALS['number_thousands_separator']);
  1585.             }
  1586.  
  1587.             return array($return_value, $unit);
  1588.         } // end of the 'PMA_formatByteDown' function
  1589.         
  1590.         
  1591.         /**
  1592.          * Extracts ENUM / SET options from a type definition string
  1593.          *
  1594.          * @param   string   The column type definition
  1595.          *
  1596.          * @return  array    The options or
  1597.          *          boolean  FALSE in case of an error.
  1598.          *
  1599.          * @author  rabus
  1600.          */
  1601.         function PMA_getEnumSetOptions($type_def) {
  1602.             $open = strpos($type_def, '(');
  1603.             $close = strrpos($type_def, ')');
  1604.             if (!$open || !$close) {
  1605.                 return FALSE;
  1606.             }
  1607.             $options = substr($type_def, $open + 2, $close - $open - 3);
  1608.             $options = explode('\',\'', $options);
  1609.             return $options;
  1610.         } // end of the 'PMA_getEnumSetOptions' function
  1611.  
  1612.  
  1613.         /**
  1614.          * Ensures a database/table/field's name is not a reserved word (for MySQL
  1615.          * releases < 3.23.6)
  1616.          *
  1617.          * @param    string   the name to check
  1618.          * @param    string   the url to go back in case of error
  1619.          *
  1620.          * @return   boolean  true if the name is valid (no return else)
  1621.          *
  1622.          * @access  public
  1623.          *
  1624.          * @author   Dell'Aiera Pol; Olivier Blin
  1625.          */
  1626.         function PMA_checkReservedWords($the_name, $error_url)
  1627.         {
  1628.             // The name contains caracters <> a-z, A-Z and "_" -> not a reserved
  1629.             // word
  1630.             if (!ereg('^[a-zA-Z_]+$', $the_name)) {
  1631.                 return TRUE;
  1632.             }
  1633.  
  1634.             // Else do the work
  1635.             $filename = 'badwords.txt';
  1636.             if (file_exists($filename)) {
  1637.                 // Builds the reserved words array
  1638.                 $fd        = fopen($filename, 'r');
  1639.                 $contents  = fread($fd, filesize($filename) - 1);
  1640.                 fclose ($fd);
  1641.                 $word_list = explode("\n", $contents);
  1642.  
  1643.                 // Do the checking
  1644.                 $word_cnt  = count($word_list);
  1645.                 for ($i = 0; $i < $word_cnt; $i++) {
  1646.                     if (strtolower($the_name) == $word_list[$i]) {
  1647.                         PMA_mysqlDie(sprintf($GLOBALS['strInvalidName'], $the_name), '', FALSE, $error_url);
  1648.                     } // end if
  1649.                 } // end for
  1650.             } // end if
  1651.         } // end of the 'PMA_checkReservedWords' function
  1652.  
  1653.  
  1654.         /**
  1655.          * Writes localised date
  1656.          *
  1657.          * @param   string   the current timestamp
  1658.          *
  1659.          * @return  string   the formatted date
  1660.          *
  1661.          * @access  public
  1662.          */
  1663.         function PMA_localisedDate($timestamp = -1, $format = '')
  1664.         {
  1665.             global $datefmt, $month, $day_of_week;
  1666.  
  1667.             if ($format == '') {
  1668.                 $format = $datefmt;
  1669.             }
  1670.  
  1671.             if ($timestamp == -1) {
  1672.                 $timestamp = time();
  1673.             }
  1674.  
  1675.             $date = ereg_replace('%[aA]', $day_of_week[(int)strftime('%w', $timestamp)], $format);
  1676.             $date = ereg_replace('%[bB]', $month[(int)strftime('%m', $timestamp)-1], $date);
  1677.  
  1678.             return strftime($date, $timestamp);
  1679.         } // end of the 'PMA_localisedDate()' function
  1680.  
  1681.  
  1682.         /**
  1683.          * Prints out a tab for tabbed navigation.
  1684.          * If the variables $link and $args ar left empty, an inactive tab is created
  1685.          *
  1686.          * @param   string  the text to be displayed as link
  1687.          * @param   string  main link file, e.g. "test.php"
  1688.          * @param   string  link arguments
  1689.          * @param   string  link attributes
  1690.          * @param   string  include '?' even though no attributes are set. Can be set empty, should be '?'.
  1691.          * @param   boolean force display TAB as active
  1692.          *
  1693.          * @return  string  two table cells, the first beeing a separator, the second the tab itself
  1694.          *
  1695.          * @access  public
  1696.          */
  1697.         function PMA_printTab($text, $link, $args = '', $attr = '', $sep = '?', $active = false) {
  1698.             global $PHP_SELF, $cfg;
  1699.             global $db_details_links_count_tabs;
  1700.  
  1701.             if (((!isset($GLOBALS['active_page']) && basename($PHP_SELF) == $link) ||
  1702.                     $active ||
  1703.                     (isset($GLOBALS['active_page']) && $GLOBALS['active_page'] == $link)
  1704.                 ) && ($text != $GLOBALS['strEmpty'] && $text != $GLOBALS['strDrop'])) {
  1705.                 $bgcolor = 'silver';
  1706.             } else {
  1707.                 $bgcolor = '#DFDFDF';
  1708.             }
  1709.  
  1710.             $db_details_links_count_tabs++;
  1711.             if (!empty($attr)) {
  1712.                 if ($cfg['LightTabs']) {
  1713.                     $attr = ' ' . $attr;
  1714.                 } else {
  1715.                     $attr = ' style="display:block" ' . $attr;
  1716.                 }
  1717.             } else {
  1718.                 if ($cfg['LightTabs']) {
  1719.                     $attr = ' ';
  1720.                 } else {
  1721.                     $attr = ' style="display:block" ';
  1722.                 }
  1723.             }
  1724.  
  1725.             if ($cfg['LightTabs']) {
  1726.                 $out = '';
  1727.                 if (strlen($link) > 0) {
  1728.                     $out .= '<nobr><a href="' . $link . $sep . $args . '"' . $attr . '>'
  1729.                          .  '<b>' . $text . '</b></a></nobr>';
  1730.                 } else {
  1731.                     $out .= '<nobr><b>' . $text . '</b></nobr>';
  1732.                 }
  1733.                 $out = '[ ' . $out . ' ]   ';
  1734.             } else {
  1735.                 $out     = "\n" . '        '
  1736.                          . '<td bgcolor="' . $bgcolor . '" align="center" width="64" nowrap="nowrap" class="tab">'
  1737.                          . "\n" . '            ';
  1738.                 if (strlen($link) > 0) {
  1739.                     $out .= '<nobr><a href="' . $link . $sep . $args . '"' . $attr . '>'
  1740.                          .  '<b>' . $text . '</b></a></nobr>';
  1741.                 } else {
  1742.                     $out .= '<nobr><b>' . $text . '</b></nobr>';
  1743.                 }
  1744.                 $out     .= "\n" . '        '
  1745.                          .  '</td>'
  1746.                          .  "\n" . '        '
  1747.                          .  '<td width="8"> </td>';
  1748.             }
  1749.  
  1750.             return $out;
  1751.         } // end of the 'PMA_printTab()' function
  1752.  
  1753.  
  1754.         /**
  1755.          * Displays a link, or a button if the link's URL is too large, to
  1756.          * accommodate some browsers' limitations
  1757.          *
  1758.          * @param  string  the URL
  1759.          * @param  string  the link message
  1760.          * @param  string  js confirmation
  1761.          *
  1762.          * @return string  the results to be echoed or saved in an array
  1763.          */
  1764.         function PMA_linkOrButton($url, $message, $js_conf)
  1765.         {
  1766.             if (strlen($url) <= 2047) {
  1767.                 $onclick_url        = (empty($js_conf) ? '' : ' onclick="return confirmLink(this, \'' . $js_conf . '\')"');
  1768.                 $link_or_button     = '        <a href="' . $url . '"' . $onclick_url . '>' . "\n"
  1769.                                     . '           ' . $message . '</a>' . "\n";
  1770.             }
  1771.             else {
  1772.                 $edit_url_parts     = parse_url($url);
  1773.                 $query_parts        = explode('&', $edit_url_parts['query']);
  1774.                 $link_or_button     = '        <form action="'
  1775.                                     . $edit_url_parts['path']
  1776.                                     . '" method="post">' . "\n";
  1777.                 reset ($query_parts);
  1778.                 while (list(, $query_pair) = each($query_parts)) {
  1779.                     list($eachvar, $eachval) = explode('=', $query_pair);
  1780.                     $link_or_button .= '            <input type="hidden" name="' . str_replace('amp;', '', $eachvar) . '" value="' . htmlspecialchars(urldecode($eachval)) . '" />' . "\n";
  1781.                 } // end while
  1782.                 
  1783.                 if (stristr($message, '<img')) {
  1784.                     $link_or_button     .= '            <input type="image" src="' . eregi_replace('^.*src="(.*)".*$', '\1', $message) . '" value="'
  1785.                                         . htmlspecialchars(eregi_replace('^.*alt="(.*)".*$', '\1', $message)) . '" />' . "\n" . '</form>' . "\n";
  1786.                 } else {
  1787.                     $link_or_button     .= '            <input type="submit" value="'
  1788.                                         . htmlspecialchars($message) . '" />' . "\n" . '</form>' . "\n";
  1789.                 }
  1790.             } // end if... else...
  1791.  
  1792.             return $link_or_button;
  1793.         } // end of the 'PMA_linkOrButton()' function
  1794.  
  1795.  
  1796.         /**
  1797.          * Returns a given timespan value in a readable format.
  1798.          *
  1799.          * @param  int     the timespan
  1800.          *
  1801.          * @return string  the formatted value
  1802.          */
  1803.         function PMA_timespanFormat($seconds)
  1804.         {
  1805.             $return_string = '';
  1806.             $days = floor($seconds / 86400);
  1807.             if ($days > 0) {
  1808.                 $seconds -= $days * 86400;
  1809.             }
  1810.             $hours = floor($seconds / 3600);
  1811.             if ($days > 0 || $hours > 0) {
  1812.                 $seconds -= $hours * 3600;
  1813.             }
  1814.             $minutes = floor($seconds / 60);
  1815.             if ($days > 0 || $hours > 0 || $minutes > 0) {
  1816.                 $seconds -= $minutes * 60;
  1817.             }
  1818.             return sprintf($GLOBALS['timespanfmt'], (string)$days, (string)$hours, (string)$minutes, (string)$seconds);
  1819.         }
  1820.  
  1821.  
  1822.         if (!function_exists('in_array')) {
  1823.             /**
  1824.              * Searches $haystack for $needle and returns TRUE if it is found in
  1825.              * the array, FALSE otherwise.
  1826.              *
  1827.              * @param  mixed    the 'needle'
  1828.              * @param  array    the 'haystack'
  1829.              *
  1830.              * @return boolean  has $needle been found or not?
  1831.              */
  1832.             function in_array($needle, $haystack) {
  1833.                 while (list(, $value) = each($haystack)) {
  1834.                     if ($value == $haystack) {
  1835.                         return TRUE;
  1836.                     }
  1837.                 }
  1838.                 return FALSE;
  1839.             }
  1840.         }
  1841.  
  1842.         /**
  1843.          * Takes a string and outputs each character on a line for itself. Used mainly for horizontalflipped display mode.
  1844.          * Takes care of special html-characters.
  1845.          * Fulfills todo-item http://sourceforge.net/tracker/index.php?func=detail&aid=544361&group_id=23067&atid=377411
  1846.          *
  1847.          * @param   string   The string
  1848.          * @param   string   The Separator (defaults to "<br />\n")
  1849.          *
  1850.          * @access  public
  1851.          * @author  Garvin Hicking <me@supergarv.de>
  1852.          * @return  string      The flipped string
  1853.          */
  1854.         function PMA_flipstring($string, $Separator = "<br />\n") {
  1855.             $format_string = '';
  1856.             $charbuff = false;
  1857.  
  1858.             for ($i = 0; $i <= strlen($string); $i++) {
  1859.                 $char = substr($string, $i, 1);
  1860.                 $append = false;
  1861.  
  1862.                 if ($char == '&') {
  1863.                     $format_string .= $charbuff;
  1864.                     $charbuff = $char;
  1865.                     $append = true;
  1866.                 } elseif (!empty($charbuff)) {
  1867.                     $charbuff .= $char;
  1868.                 } elseif ($char == ';' && !empty($charbuff)) {
  1869.                     $format_string .= $charbuff;
  1870.                     $charbuff = false;
  1871.                     $append = true;
  1872.                 }
  1873.                 else
  1874.                 {
  1875.                     $format_string .= $char;
  1876.                     $append = true;
  1877.                 }
  1878.  
  1879.                 if ($append && ($i != strlen($string))) {
  1880.                     $format_string .= $Separator;
  1881.                 }
  1882.             }
  1883.  
  1884.             return $format_string;
  1885.         }
  1886.  
  1887.  
  1888.         /**
  1889.          * Function added to avoid path disclosures.
  1890.          * Called by each script that needs parameters, it displays
  1891.          * an error message and, by defaults, stops the execution. 
  1892.          *
  1893.          * @param   array   The names of the parameters needed by the calling
  1894.          *                  script.
  1895.          * @param   boolean Stop the execution?
  1896.          *                  (Set this manually to FALSE in the calling script
  1897.          *                   until you know all needed parameters to check).
  1898.          *
  1899.          * @access  public
  1900.          * @author  Marc Delisle (lem9@users.sourceforge.net)
  1901.          */
  1902.         function PMA_checkParameters($params, $die = TRUE) {
  1903.             global $PHP_SELF;
  1904.  
  1905.             $reported_script_name = basename($PHP_SELF);
  1906.             $found_error = FALSE;
  1907.             $error_message = '';
  1908.  
  1909.             while (list(, $param) = each($params)) {
  1910.                 if (!isset($GLOBALS[$param])) {
  1911.                     $error_message .= $reported_script_name . ': Missing ' . $param . '<br />';
  1912.                     $found_error = TRUE;
  1913.                 }
  1914.             }
  1915.             if ($found_error) {
  1916.                 include('./libraries/header_meta_style.inc.php');
  1917.                 echo '</head><body><p>' . $error_message . '</p></body></html>';
  1918.                 if ($die) {
  1919.                     exit();
  1920.                 }
  1921.             }
  1922.         } // end function
  1923.  
  1924.  
  1925.         // Kanji encoding convert feature appended by Y.Kawada (2002/2/20)
  1926.         if (PMA_PHP_INT_VERSION >= 40006
  1927.             && @function_exists('mb_convert_encoding')
  1928.             && strpos(' ' . $lang, 'ja-')
  1929.             && file_exists('./libraries/kanji-encoding.lib.php')) {
  1930.             include('./libraries/kanji-encoding.lib.php');
  1931.             define('PMA_MULTIBYTE_ENCODING', 1);
  1932.         } // end if
  1933.  
  1934.         // garvin: moved from read_dump.php because this should be used in tbl_replace_fields.php as well.
  1935.         if (!function_exists('is_uploaded_file')) {
  1936.             /**
  1937.              * Emulates the 'is_uploaded_file()' function for old php versions.
  1938.              * Grabbed at the php manual:
  1939.              *     http://www.php.net/manual/en/features.file-upload.php
  1940.              *
  1941.              * @param   string    the name of the file to check
  1942.              *
  1943.              * @return  boolean   wether the file has been uploaded or not
  1944.              *
  1945.              * @access  public
  1946.              */
  1947.             function is_uploaded_file($filename) {
  1948.                 if (!$tmp_file = @get_cfg_var('upload_tmp_dir')) {
  1949.                     $tmp_file = tempnam('','');
  1950.                     $deleted  = @unlink($tmp_file);
  1951.                     $tmp_file = dirname($tmp_file);
  1952.                 }
  1953.                 $tmp_file     .= '/' . basename($filename);
  1954.  
  1955.                 // User might have trailing slash in php.ini...
  1956.                 return (ereg_replace('/+', '/', $tmp_file) == $filename);
  1957.             } // end of the 'is_uploaded_file()' emulated function
  1958.         } // end if
  1959.     } // end if: minimal common.lib needed?
  1960. } // $__PMA_COMMON_LIB__
  1961. ?>
  1962.